Are there any other kinds of loops besides counting loops?
Yes. Many loops don't have a counter. For example, a loop might keep on going until some complicated goal has been achieved.
Loops that are not counting loops must use the
DO WHILE
statement
(or an equivalent.)
For example,
here is a program that asks the user for a number,
then types out its square root.
The user can do this for as many numbers as desired.
Since it is not known in advance how many numbers this will be,
a DO WHILE
loop must be used.
' Print out the square root of each number entered. ' A negative number is entered to exit the program. ' PRINT "Enter a positive number" INPUT NUM ' DO WHILE NUM >= 0 PRINT "The square root of", NUM, "is", SQR( NUM ) PRINT "Enter a positive number" INPUT NUM LOOP ' END
This is not a counting loop, so the FOR
statement
is not appropriate.
Are there times when you count, but count using a different pattern than "1, 2, 3, 4, ...."?